package com.example.firstapp.KtLesson

object StringUtil {
    fun lettersCount(str: String): Int {
        var count = 0
        for (char in str) {
            if (char.isLetter()) {
                count++
            }
        }
        return count
    }
}


fun main() {
    // 普通实现
    val str = "ABC123xyz!@#"
    val count1 = StringUtil.lettersCount(str)


    // 拓展函数  直接给String拓展一个新的函数，然后通过str<String>.调用  lettersCount()也不用接收参数
    fun String.lettersCount(): Int {
        var count = 0
        for (char in this) {
            if (char.isLetter()) {
                count++
            }
        }
        return count
    }
    val count2 = str.lettersCount()
    println(count1)
    println(count2)
}




